package com.yl.chainStore.entity.warehouse

import com.baomidou.mybatisplus.annotation.IdType
import com.baomidou.mybatisplus.annotation.TableField
import com.baomidou.mybatisplus.annotation.TableId
import com.baomidou.mybatisplus.annotation.TableName
import com.yl.chainStore.common.Constants
import com.yl.chainStore.common.enums.Status
import com.yl.common.annotation.NoArg
import java.math.BigDecimal
import java.math.RoundingMode

/**
 * <p>
 *  损益单明细
 * </p>
 *
 * @author cxw
 * @since 2021-07-22
 */
@NoArg
@TableName("warehouse_profit_loss_order_detail")
data class WarehouseProfitLossOrderDetail(

	/**
	 * 损益单明细
	 */
	@TableId(value = "id", type = IdType.AUTO)
	var id: Long,

	/**
	 * 批次id
	 */
	@TableField("batch_count_id")
	var batchCountId: Long? = null,

	/**
	 * 数量
	 */
	@TableField("good_count")
	var goodCount: Int = 0,

	/**
	 * 项目
	 */
	@TableField("item_id")
	var itemId: Long,

	@TableField(exist = false)
	var itemName:String?= null,

	@TableField(exist = false)
	var unit:String?= null,

	@TableField(exist = false)
	var specification:String?= null,
	@TableField(exist = false)
	var brand:String?= null,
	@TableField(exist = false)
	var nickname:String?= null,



	/**
	 * 单价
	 */
	@TableField("price")
	var price: Long,

	/**
	 * 状态备用
	 */
	@TableField("status")
	var status: Status = Status.UNKNOWN,

	/**
	 * 订单id
	 */
	@TableField("order_id")
	var orderId:Long = 0,

	/**
	 * 备注
	 */
	@TableField("remark")
	var remark:String ?= null,
){
	/**
	 * 实际要显示的实际总金额
	 */
	@TableField(exist = false)
	var sumMoneyShow: BigDecimal?= null
		get() {
			if(field == null ){
				val sumMoney = price * goodCount
				return BigDecimal(sumMoney.div(Constants.OtherConfig.MONEY_TO_DB_DOUBLE)).setScale(2, RoundingMode.HALF_UP)
			}
			return field
		}
		set(value) {
			if(value != null){
				val sumMoney = (value.multiply(BigDecimal(Constants.OtherConfig.MONEY_TO_DB_DOUBLE))).toLong()
				price = sumMoney/goodCount
				field = value
			}
		}

	/**
	 * 实际要显示的单价
	 */
	@TableField(exist = false)
	var realGoodPrice: BigDecimal?= null
		get() {
			if(field == null){
				return BigDecimal(price.div(Constants.OtherConfig.MONEY_TO_DB_DOUBLE)).setScale(2, RoundingMode.HALF_UP)
			}
			return field
		}
		set(value) {
			if(value != null){
				price = (value.multiply(BigDecimal(Constants.OtherConfig.MONEY_TO_DB_DOUBLE))).toLong()
				field = value
			}
		}

	/**
	 * 均价，前端传值和显示用
	 */
	@TableField(exist = false)
	var averagePrice:BigDecimal?=null
		get() {
			if(field == null){
				return BigDecimal(price.div(Constants.OtherConfig.MONEY_TO_DB_DOUBLE)).setScale(2, RoundingMode.HALF_UP)
			}
			return field
		}
		set(value) {
			if(value != null){
				price = (value.multiply(BigDecimal(Constants.OtherConfig.MONEY_TO_DB_DOUBLE))).toLong()
				field = value
			}
		}
}
